package com.doublefish.doublefishrobot.ui.home.widget;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.doublefish.doublefishrobot.R;
import com.doublefish.doublefishrobot.utils.AppCacheUtil;

public class SafeDialog extends Dialog implements View.OnClickListener {

    public static final int S8_TIPS_MAX_SPEED = 60;
    public static final int S6_TIPS_MAX_SPEED = 15;
    public static final int S5_TIPS_MAX_SPEED = 6;

//    private TextView titleTxt;

    private TextView checkTV;
    private ImageView checkIV;

    private TextView submitTV;
    private TextView cancelTV;

    private OnDialogListner listener;
    private boolean isIgnoreSpeedTips = AppCacheUtil.getInstance().isIgnoreSpeedTips();

    public static boolean shouldShowSafeDialog() {
        return !AppCacheUtil.getInstance().isIgnoreSpeedTips();
    }

    public SafeDialog(Context context, OnDialogListner listener) {
        super(context,  R.style.dialog);
        this.listener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_safe_tips);
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        checkTV = findViewById(R.id.tv_check);
        checkIV = findViewById(R.id.iv_tick);
        submitTV = findViewById(R.id.submit);
        cancelTV = findViewById(R.id.cancel);

        submitTV.setOnClickListener(this);
        cancelTV.setOnClickListener(this);
        checkTV.setOnClickListener(this);

        checkIV.setVisibility(isIgnoreSpeedTips ? View.VISIBLE : View.INVISIBLE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                if (listener != null) {
                    listener.onCancelBtnClick();
                }
                this.dismiss();
                break;
            case R.id.submit:
                if (listener != null) {
                    listener.onComfirmBtnClick();
                }
                dismiss();
                break;

            case R.id.tv_check:
                checkTVchanged();
                break;
        }
    }

    private void checkTVchanged() {
        isIgnoreSpeedTips = !isIgnoreSpeedTips;
        AppCacheUtil.getInstance().setIgnoreSpeedTips(isIgnoreSpeedTips);
        checkIV.setVisibility(isIgnoreSpeedTips ? View.VISIBLE : View.INVISIBLE);
    }

    //region OnDialogListner

    public interface OnDialogListner {
        void onComfirmBtnClick();
        void onCancelBtnClick();
    }

    //endregion
}